package cn.heycm.tenant.user.repository;

import cn.heycm.tenant.common.annotation.Tenant;
import cn.heycm.tenant.user.repository.entity.UserProfile;
import cn.heycm.tenant.user.repository.mapper.UserProfileMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hey
 * @version 1.0
 * @since 2024/8/24 22:45
 */
@Component
@RequiredArgsConstructor
public class UserRepository {

    private final UserProfileMapper userProfileMapper;

    private AtomicInteger count = new AtomicInteger(0);

    public UserProfile getById(Integer id) {
        return userProfileMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void addName(String name) {
        int i = count.incrementAndGet();
        UserProfile profile = new UserProfile();
        profile.setUserName(name + " - " + i);
        userProfileMapper.insertSelective(profile);
        if (i % 2 == 0) {
            throw new RuntimeException("测试事务");
        }
    }
}
